
#import "uiglobe.h"
#import "globe.h"

@implementation UIGlobe 

- (void) applicationDidFinishLaunching: (id) unused
{
    struct CGRect rect = [UIHardware fullScreenApplicationContentRect];
    rect.origin.x = rect.origin.y = 0.0f;

    window = [[UIWindow alloc] initWithContentRect: rect];

    [window orderFront: self];
    [window makeKey: self];
    [window _setHidden: NO];

    mainView = [[UIView alloc] initWithFrame: rect];
    [window setContentView: mainView];

/*
    textView = [[UITextView alloc]
        initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 240.0f)];
    [textView setEditable:YES];
    [textView setTextSize:14];

    kb = [[UIKeyboard alloc]
        initWithFrame:CGRectMake(0.0f, 245.0f, 320.0f, 200.0f)];

    [mainView addSubview:textView];
    [mainView addSubview:kb];

    path = @"/var/root/.s1text";

    [textView setText:
        [NSMutableString
            stringWithContentsOfFile:path
            encoding:NSMacOSRomanStringEncoding
            error:&error]];
*/

    timer = [NSTimer scheduledTimerWithTimeInterval: 0.01
                     target: self
                     selector: @selector(fireTimer:)
                     userInfo: nil
                     repeats: YES];

    globe_start(rect.size.width,rect.size.height);
}

- (void)fireTimer:(NSTimer*)timer
{
   globe_event(GLOBE_UPDATE,0,0);
}

- (void) applicationWillExit
{
   globe_stop();
  [window release];
}

struct CGRect GSEventGetLocationInWindow(void *ev);

- (void) mouseDown:(void*)event {
  CGRect loc = GSEventGetLocationInWindow(event);
  globe_event(GLOBE_MOUSEDOWN,loc.origin.x,loc.origin.y);
}

- (void) mouseDragged:(void*)event {
  CGRect loc = GSEventGetLocationInWindow(event);
  globe_event(GLOBE_MOUSEDRAG,loc.origin.x,loc.origin.y);
}

- (void) mouseUp:(void*)event {
  CGRect loc = GSEventGetLocationInWindow(event);
  globe_event(GLOBE_MOUSEUP,loc.origin.x,loc.origin.y);
}

- (void) applicationWillSuspend
{
   globe_stop();
/*
   [[textView text]
        writeToFile: path
        atomically: NO
        encoding: NSMacOSRomanStringEncoding
        error: &error];
*/
}

@end

